<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * ajax上传
 */
namespace Addon\WeiXin\Controller;
use Addon\WeiXin\Controller\CommonController;

/**
 * AjaxUpload上传插件 TODO上传其他类型文件
 */
class PayController extends CommonController {
    
	
	//支付订单 TODO订单类型 商城订单和充值订单的区分
	public function Order(){
		
		
		$weixin = getWeiXinConf();
		
		$appid = $weixin['weixinpay_appid'];
		$appsecret = $weixin['weixinpay_appsecret'];
		$mch_id = $weixin['weixinpay_mchid'];
		$paykey = $weixin['weixinpay_paykey'];
		$access = \Org\Net\Wechat\Oauth::token($appid,$appsecret);
		
		
		$order_id = I('param.id');
		
		$m_pay_order = M('pay_order');
		
		
		$map['order_id'] = $order_id;
		$map['status'] = 2;

		$data = $m_pay_order->where($map)->find();
		
		if(!$data){
			
			
			$this->error('找不到订单信息');die;
				
		}
		
	
		
		$notify = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/Home/Pay/Notify/Addon/WeiXin.html';
		
		$order = \Org\Net\Wechat\Pay::unifiedOrder($mch_id,$appid,$paykey,$order_id,$data['name'],$data['money'],'JSAPI',$notify,$access['openid']);
		
		$this->assign('order',$order);
		$this->assign('data',$data);
		
		
			
		$this->view();
	}
	
	//统一接受微信回调信息
	public function Notify(){
		
		$msg = array();

		$postStr = file_get_contents('php://input');

		$msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		
		$m_pay_order = M('pay_order');
	
		
		$map['order_id'] = $msg['out_trade_no'];
		$data['success_time'] = time();
		$data['success_ip'] = get_client_ip();
		$data['type'] = 2;
		$data['status'] = 1;
		
		
		$result = $m_pay_order->where($map)->save($data);
		
		
				
		if($result){ 
		
		
			$return ="<xml>
					  <return_code>SUCCESS</return_code>
					  <return_msg>OK</return_msg>
					</xml>"; 
				  
			
		} else {   
			$return ="<xml>
					  <return_code>FAIL</return_code>
					  <return_msg>NOOK</return_msg>
					</xml>";
			
		}
		
		return $return;
		
	}
	
	
	//统一接受ajxa页面回调信息
	public function Info(){
		
	
		$order_id = I('param.order_id');
			
				//增加订单信息
		$m_pay_order = M('pay_order');
		
		$map['order_id'] = I('param.order_id');
		$map['status'] = 1;
		$data = $m_pay_order->where($map)->find();
		
		if($data){
			
			echo 1;
		}
	
		
	}
	
	
	
	
	
}